# fixture pystest的夹具
# 夹具的命名不能是test开头,还要添加一个声明 @pytest.fixture
from time import sleep

import pytest
from selenium import  webdriver
from selenium.webdriver.support.wait import WebDriverWait  # 显示等待
from selenium.webdriver.support import  expected_conditions
from selenium.webdriver.common.by import By

class TestOA():
    driver = None
    @pytest.fixture
    def get_driver(self):
        driver = webdriver.Chrome()
        driver.get("http://192.168.1.138/ranzhi/www/sys/index.html")
        driver.maximize_window()
        driver.find_element(By.XPATH, '//*[@id="account"]').send_keys("admin")
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("123456")
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        driver = get_driver
        driver.find_element(By.XPATH, '//*[@id="s-menu-superadmin"]/button/i').click()
        driver.switch_to.frame("iframe-superadmin")  # 进入框架iframe-superadmin
        WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a')))
        driver.find_element(By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a').click()
        yield driver
        driver.quit()

    # def test_driver_login(self,get_driver):
    #     driver = get_driver
    #     driver.find_element(By.XPATH, '//*[@id="s-menu-superadmin"]/button/i').click()
    #     driver.switch_to.frame("iframe-superadmin")  # 进入框架iframe-superadmin
    #     WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a')))
    #     driver.find_element(By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a').click()

    def test_username_fail1(self,get_driver):
        driver = get_driver

        WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        driver.find_element(By.ID, "account").send_keys("")
        sleep(3)
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        text = driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        assert text == "用户名不能为空。;用户名应当为字母或数字的组合，至少三位", "输入空的用户名进行保存,用例执行失败"
        driver.quit()

    def test_username_fail2(self,get_driver):
        driver = get_driver
        WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "account")))
        driver.find_element(By.ID, "account").send_keys("2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
        sleep(3)
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        sleep(3)
        text = driver.find_element(By.XPATH, '//*[@id="accountLabel"]').text
        assert text == "用户名长度应当不超过30，且不小于0。", "输入2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222用户名进行保存,用例执行失败"
        driver.quit()

#
# if __name__ == '__main__':
#     a = TestOA
#     a.username_fail1()